-- FUNCTION: public.udf_doctor_new_revenue(integer, integer, date, date)

 DROP FUNCTION public.udf_doctor_new_revenue(integer, integer, date, date);

CREATE OR REPLACE FUNCTION public.udf_doctor_new_revenue(
	providerid integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer,
	"fromDate" date DEFAULT NULL::date,
	"toDate" date DEFAULT NULL::date)
    RETURNS TABLE("AccountId" integer, "DoctorName" text, "AppointmentCashTotal" numeric, "AppointmentCardTotal" numeric, "AppointmentUPITotal" numeric, "AppointmentOnlineTotal" numeric, "AppointmentChequeTotal" numeric, "AppointmentPaytmTotal" numeric, "AppointmentAmount" numeric, "AdmissionCashTotal" numeric, "AdmissionCardTotal" numeric, "AdmissionUPITotal" numeric, "AdmissionOnlineTotal" numeric, "AdmissionChequeTotal" numeric, "AdmissionPaytmTotal" numeric, "AdmissionAmount" numeric, "LabCash" numeric, "LabCard" numeric, "LabUPI" numeric, "LabOnline" numeric, "LabCheque" numeric, "LabPaytm" numeric, "LabAmount" numeric, "PharmacySaleCash" numeric, "PharmacySaleCard" numeric, "PharmacySaleUPI" numeric, "PharmacySaleOnline" numeric, "PharmacySaleCheque" numeric, "PharmacySalePaytm" numeric, "PharmacyAmount" numeric, "TotalCash" numeric, "TotalCard" numeric, "TotalUPI" numeric, "TotalOnline" numeric, "TotalCheque" numeric, "TotalPaytm" numeric, "Total" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
  return query 

with accountdata as (
select P."ProviderId",P."FullName" "DoctorName"
from "Provider" P
	join "ProviderLocation" PL on PL."ProviderId"=P."ProviderId"
where case when providerId is null then 1=1 else P."ProviderId"=providerId end 
	and case when "locationId" is null then 1=1 else PL."LocationId"= "locationId" end   
)

,actappointmentamount as (
	
	select a."ProviderId",sum(A."CashTotal") "AppointmentCashTotal",sum(A."CardTotal") "AppointmentCardTotal",
	sum(A."UPITotal") "AppointmentUPITotal",sum(A."OnlineTotal") "AppointmentOnlineTotal",
	sum(A."ChequeTotal") "AppointmentChequeTotal",sum(A."PaytmTotal") "AppointmentPaytmTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal") appointmentamount 
	from (
   select  ap."ProviderId"  "ProviderId",   
   case when A."PayTypeId"=1 then coalesce(A."Cost",0) else 0 end  "CashTotal" ,
   case when A."PayTypeId"=2 then coalesce(A."Cost",0) else 0 end "CardTotal" ,
	case when A."PayTypeId"=3 then coalesce(A."Cost",0) else 0 end  "UPITotal" ,
   case when A."PayTypeId"=4 then coalesce(A."Cost",0) else 0 end "OnlineTotal",
case when A."PayTypeId"=5 then coalesce(A."Cost",0) else 0 end  "ChequeTotal" ,
   case when A."PayTypeId"=6 then coalesce(A."Cost",0) else 0 end "PaytmTotal"											
		from "Receipt" A
    join "Appointment" ap on  ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
    and ap."Active" =true	
   where 
	case when providerId is null then 1=1 else ap."ProviderId"=providerId end   and 
											A."ReceiptTypeId"=1 	
	and A."Active"<>false and A."RespectiveId" is not null  
AND CASE WHEN ("locationId" IS NULL OR "locationId"=0) THEN 1=1 ELSE ap."LocationId"="locationId" END 
	and coalesce(A."IsRefunded",false) <>true and ap."Status" <> 'C' 
	and	case when "fromDate" is null then 1=1 
	else (A."CreatedDate"::date >= "fromDate" and A."CreatedDate"::date <= "toDate")  end	
	)a		
	group by  a."ProviderId"		
)
,refundappointmentamount as (
	
	select a."ProviderId" ,
	sum(A."CashTotal") "AppointmentRefCashTotal",sum(A."CardTotal") "AppointmentRefCardTotal",
	sum(A."UPITotal") "AppointmentRefUPITotal",sum(A."OnlineTotal") "AppointmentRefOnlineTotal",
	sum(A."ChequeTotal") "AppointmentRefChequeTotal",sum(A."PaytmTotal") "AppointmentRefPaytmTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal") refappointmentamount
	from (
    select  ap."ProviderId"  "ProviderId",
		   case when A."PayTypeId"=1 then coalesce(A."Cost",0) else 0 end  "CashTotal" ,
          case when A."PayTypeId"=2 then coalesce(A."Cost",0) else 0 end "CardTotal",
			case when A."PayTypeId"=3 then coalesce(A."Cost",0) else 0 end  "UPITotal" ,
          case when A."PayTypeId"=4 then coalesce(A."Cost",0) else 0 end "OnlineTotal",								
		case when A."PayTypeId"=5 then coalesce(A."Cost",0) else 0 end  "ChequeTotal" ,
          case when A."PayTypeId"=6 then coalesce(A."Cost",0) else 0 end "PaytmTotal"
		 from "Receipt" A
    join "Appointment" ap on ap."AppointmentId" =A."RespectiveId"  
	join "Provider" pr on pr."ProviderId"= ap."ProviderId"
   and ap."Status" <> 'C'   and ap."Active" =true
	
   where case when providerId is null then 1=1 else ap."ProviderId"=providerId end    
	
	and A."Active"<>false and A."RespectiveId" is not null 
AND CASE WHEN ("locationId" IS NULL OR "locationId"=0) THEN 1=1 ELSE ap."LocationId"="locationId" END  
	and coalesce(A."IsRefunded",false) = true 
	and case when "fromDate" is null then 1=1 
	else (A."CreatedDate"::date >= "fromDate" and A."CreatedDate"::date <= "toDate")  end	
	) a
	group by  a."ProviderId"	
	
)

,appointmentamount as (
select  A."ProviderId",
	coalesce(A."AppointmentCashTotal",0) - coalesce(B."AppointmentRefCashTotal",0) as "AppointmentCashTotal" ,
	coalesce(A."AppointmentCardTotal",0) - coalesce(B."AppointmentRefCardTotal",0) as "AppointmentCardTotal" ,	
	coalesce(A."AppointmentUPITotal",0) - coalesce(B."AppointmentRefUPITotal",0) as "AppointmentUPITotal" ,
	coalesce(A."AppointmentOnlineTotal",0) - coalesce(B."AppointmentRefOnlineTotal",0) as "AppointmentOnlineTotal" ,
	coalesce(A."AppointmentChequeTotal",0) - coalesce(B."AppointmentRefChequeTotal",0) as "AppointmentChequeTotal" ,
	coalesce(A."AppointmentPaytmTotal",0) - coalesce(B."AppointmentRefPaytmTotal",0) as "AppointmentPaytmTotal" ,
	
	coalesce(A.appointmentamount,0) - coalesce(B.refappointmentamount,0) as "AppointmentAmount" 
	from actappointmentamount A
	left join refundappointmentamount B on A."ProviderId"=B."ProviderId"		
)

,
actadmissionamount as (
	
	select a."ProviderId" , sum(A."CashTotal") "AdmissionCashTotal",sum(A."CardTotal") "AdmissionCardTotal",
	sum(A."UPITotal") "AdmissionUPITotal",sum(A."OnlineTotal") "AdmissionOnlineTotal",
	sum(A."ChequeTotal") "AdmissionChequeTotal",sum(A."PaytmTotal") "AdmissionPaytmTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal")+ sum(A."ChequeTotal")+ sum(A."PaytmTotal") "AdmissionAmount" 
	from (
select ad."ProviderId" "ProviderId" ,
   case when adr."PayTypeId"=1 then coalesce(adr."Cost",0) else 0 end  "CashTotal" ,
   case when adr."PayTypeId"=2 then coalesce(adr."Cost",0) else 0 end "CardTotal",									  
	case when adr."PayTypeId"=3 then coalesce(adr."Cost",0) else 0 end  "UPITotal" ,
   case when adr."PayTypeId"=4 then coalesce(adr."Cost",0) else 0 end "OnlineTotal",		
case when adr."PayTypeId"=5 then coalesce(adr."Cost",0) else 0 end  "ChequeTotal" ,
   case when adr."PayTypeId"=6 then coalesce(adr."Cost",0) else 0 end "PaytmTotal"											
from  "Receipt" adr 	 
	 join "Admission" ad on  adr."AdmissionId" = ad."AdmissionId" and ad."Active" <> false
	join "Provider" pr on pr."ProviderId"= ad."ProviderId"
	where case when providerId is null then 1=1 else ad."ProviderId"=providerId end  	and
		adr."ReceiptTypeId"=1  and adr."Active"<>false  and adr."AdmissionId" is not null
AND CASE WHEN ("locationId" IS NULL OR "locationId"=0) THEN 1=1 ELSE ad."LocationId"="locationId" END 
	and case when "fromDate" is null then 1=1 
	else (adr."CreatedDate"::date >= "fromDate" and adr."CreatedDate"::date <= "toDate")  end	
	)a
	group by  a."ProviderId"	
)

,refadmissionamount as (
	select a."ProviderId" , sum(A."CashTotal") "AdmissionRefCashTotal",sum(A."CardTotal") "AdmissionRefCardTotal",
	sum(A."UPITotal") "AdmissionRefUPITotal",sum(A."OnlineTotal") "AdmissionRefOnlineTotal",
	sum(A."ChequeTotal") "AdmissionRefChequeTotal",sum(A."PaytmTotal") "AdmissionRefPaytmTotal",
	sum(A."CashTotal")+ sum(A."CardTotal")+ sum(A."UPITotal")+ sum(A."OnlineTotal") + sum(A."ChequeTotal")+ sum(A."PaytmTotal") "AdmissionRefAmount" 
	from (
select ad."ProviderId" "ProviderId"  ,  
		case when adr."PayTypeId"=1 then coalesce(adr."Cost",0) else 0 end  "CashTotal" ,
        case when adr."PayTypeId"=2 then coalesce(adr."Cost",0) else 0 end "CardTotal" ,
		case when adr."PayTypeId"=3 then coalesce(adr."Cost",0) else 0 end  "UPITotal" ,
        case when adr."PayTypeId"=4 then coalesce(adr."Cost",0) else 0 end "OnlineTotal" ,
		case when adr."PayTypeId"=5 then coalesce(adr."Cost",0) else 0 end  "ChequeTotal" ,
        case when adr."PayTypeId"=6 then coalesce(adr."Cost",0) else 0 end "PaytmTotal" 									
from  "Receipt" adr 	 
	 join "Admission" ad on  adr."AdmissionId" = ad."AdmissionId" and ad."Active" <> false
	join "Provider" pr on pr."ProviderId"= ad."ProviderId"
	where case when providerId is null then 1=1 else ad."ProviderId"=providerId end  and 
		adr."IsRefunded" = true   and adr."Active"<>false  and adr."AdmissionId" is not null
AND CASE WHEN ("locationId" IS NULL OR "locationId"=0) THEN 1=1 ELSE ad."LocationId"="locationId" END 
	and case when "fromDate" is null then 1=1 
	else (adr."CreatedDate"::date >= "fromDate" and adr."CreatedDate"::date <= "toDate")  end	
	)a
	group by  a."ProviderId"	
)
,admissionamount as (
select A."ProviderId" ,
	coalesce(A."AdmissionCashTotal",0) - coalesce(B."AdmissionRefCashTotal",0) "AdmissionCashTotal",
	coalesce(A."AdmissionCardTotal",0) - coalesce(B."AdmissionRefCardTotal",0) "AdmissionCardTotal",
	coalesce(A."AdmissionUPITotal",0) - coalesce(B."AdmissionRefUPITotal",0) "AdmissionUPITotal",
	coalesce(A."AdmissionOnlineTotal",0) - coalesce(B."AdmissionRefOnlineTotal",0) "AdmissionOnlineTotal",
	coalesce(A."AdmissionChequeTotal",0) - coalesce(B."AdmissionRefChequeTotal",0) "AdmissionChequeTotal",
	coalesce(A."AdmissionPaytmTotal",0) - coalesce(B."AdmissionRefPaytmTotal",0) "AdmissionPaytmTotal",
	coalesce(A."AdmissionAmount",0) - coalesce(B."AdmissionRefAmount",0) "AdmissionAmount"
   from actadmissionamount A
	left join refadmissionamount B on A."ProviderId"=B."ProviderId"	 
)

,LabAmount as ( 
select a."ProviderId",sum(a."Cash") "LabCash",sum(a."Card") "LabCard" ,
sum(a."UPI") "LabUPI",sum(a."Online") "LabOnline" ,
sum(a."Cheque") "LabCheque",sum(a."Paytm") "LabPaytm" ,
sum(a."Cash")+sum(a."Card") +sum(a."UPI")+sum(a."Online")+sum(a."Cheque")+sum(a."Paytm")"LabAmount"  
from(
select 	a."ProviderId",							  
	case when ar."PayTypeId"=1 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cash"
	,case when ar."PayTypeId"=2 then  coalesce(ar."OverallNetAmount",0) else 0 end "Card"
	,case when ar."PayTypeId"=3 then  coalesce(ar."OverallNetAmount",0) else 0 end "UPI"
	,case when ar."PayTypeId"=4 then  coalesce(ar."OverallNetAmount",0) else 0 end "Online"
	,case when ar."PayTypeId"=5 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cheque"
	,case when ar."PayTypeId"=6 then  coalesce(ar."OverallNetAmount",0) else 0 end "Paytm"
from "Provider" a
join "NewLabBookingHeader" ar on ar."DoctorId"=a."ProviderId" and ar."Active" <> false

 where case when providerId is null then 1=1 else a."ProviderId"=providerId end
AND CASE WHEN ("locationId" IS NULL OR "locationId"=0) THEN 1=1 ELSE ar."LocationId"="locationId" END    
	and case when "fromDate" is null then 1=1 
	else (ar."CreatedDate"::date >= "fromDate" and ar."CreatedDate"::date <= "toDate")  end
)a
group by a."ProviderId" )

, PharmaSaleAmount as (	
select a."ProviderId", sum(a."Cash") "PharmaSaleCash",sum(a."Card") "PharmaSaleCard"
	,sum(a."UPI") "PharmaSaleUPI",sum(a."Online") "PharmaSaleOnline"
	,sum(a."Cheque") "PharmaSaleCheque",sum(a."Paytm") "PharmaSalePaytm"
	, sum(a."Cash")+ sum(a."Card")+ sum(a."UPI")+ sum(a."Online")+ sum(a."Cheque")+ sum(a."Paytm") "PharmaSaleAmount" 
	from (
select a."ProviderId", 
	 case when ar."PayTypeId"=1 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cash"
	,case when ar."PayTypeId"=2 then  coalesce(ar."OverallNetAmount",0) else 0 end "Card"
	,case when ar."PayTypeId"=3 then  coalesce(ar."OverallNetAmount",0) else 0 end "UPI"
	,case when ar."PayTypeId"=4 then  coalesce(ar."OverallNetAmount",0) else 0 end "Online"
	,case when ar."PayTypeId"=5 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cheque"
	,case when ar."PayTypeId"=6 then  coalesce(ar."OverallNetAmount",0) else 0 end "Paytm"	
from "Provider" a
join "PharmacySaleHeader" ar on ar."ProviderId"=a."ProviderId"
where case when providerId is null then 1=1 else a."ProviderId"=providerId end 
AND CASE WHEN ("locationId" IS NULL OR "locationId"=0) THEN 1=1 ELSE ar."LocationId"="locationId" END 
and case when "fromDate" is null then 1=1 
else (ar."SaleDate"::date >= "fromDate" and ar."SaleDate"::date <= "toDate")  end	
	)a
group by a."ProviderId"
)

,PharmaReturnAmount as (
select a."ProviderId", sum(a."Cash") "PharmaReturnCash",sum(a."Card") "PharmaReturnCard" 
	,sum(a."UPI") "PharmaReturnUPI",sum(a."Online") "PharmaReturnOnline" 
	,sum(a."Cheque") "PharmaReturnCheque",sum(a."Paytm") "PharmaReturnPaytm" 
	,sum(a."Cash")+sum(a."Card")+sum(a."UPI")+sum(a."Online")+sum(a."Cheque")+sum(a."Paytm")   "PharmaReturnAmount" 
	from(
select a."ProviderId", 
	case when ar."PayTypeId"=1 then  coalesce(srh."OverallNetAmount",0) else 0 end "Cash"
	,case when ar."PayTypeId"=2 then  coalesce(srh."OverallNetAmount",0) else 0 end "Card"
	,case when ar."PayTypeId"=3 then  coalesce(srh."OverallNetAmount",0) else 0 end "UPI"
	,case when ar."PayTypeId"=4 then  coalesce(srh."OverallNetAmount",0) else 0 end "Online"
	,case when ar."PayTypeId"=5 then  coalesce(srh."OverallNetAmount",0) else 0 end "Cheque"
	,case when ar."PayTypeId"=6 then  coalesce(srh."OverallNetAmount",0) else 0 end "Paytm"							  				   
from "Provider" a
join "PharmacySaleHeader" ar on ar."ProviderId"=a."ProviderId"
left join "SaleReturnHeader" srh on srh."PharmacySaleHeaderId" = ar."PharmacySaleHeaderId"
where case when providerId is null then 1=1 else a."ProviderId"=providerId end  
AND CASE WHEN ("locationId" IS NULL OR "locationId"=0) THEN 1=1 ELSE ar."LocationId"="locationId" END 
and case when "fromDate" is null then 1=1 
else (srh."ReturnDate"::date >= "fromDate" and srh."ReturnDate"::date <= "toDate")  end	
	)a
group by a."ProviderId"
)

,PharmaAmount as (
select a."ProviderId", 
	
	 coalesce(a."PharmaSaleCash",0) - coalesce(b. "PharmaReturnCash",0) as  "PharmaSaleCash" ,
	 coalesce(a."PharmaSaleCard",0) - coalesce(b. "PharmaReturnCard",0) as  "PharmaSaleCard" ,
	 coalesce(a."PharmaSaleUPI",0) - coalesce(b. "PharmaReturnUPI",0) as  "PharmaSaleUPI" ,
	 coalesce(a."PharmaSaleOnline",0) - coalesce(b. "PharmaReturnOnline",0) as  "PharmaSaleOnline" ,
	 coalesce(a."PharmaSaleCheque",0) - coalesce(b. "PharmaReturnCheque",0) as  "PharmaSaleCheque" ,
	 coalesce(a."PharmaSalePaytm",0) - coalesce(b. "PharmaReturnPaytm",0) as  "PharmaSalePaytm" ,
	coalesce(a."PharmaSaleAmount",0) - coalesce(b. "PharmaReturnAmount",0) as  "PharmaAmount" 
from PharmaSaleAmount a
	left join PharmaReturnAmount b on a."ProviderId"=b."ProviderId"
)
	
select distinct  a."ProviderId",a."DoctorName"::text "DoctorName",
						  ap."AppointmentCashTotal",ap."AppointmentCardTotal",ap."AppointmentUPITotal",ap."AppointmentOnlineTotal", ap."AppointmentChequeTotal",ap."AppointmentPaytmTotal", ap."AppointmentAmount",
						  ad."AdmissionCashTotal",ad."AdmissionCardTotal",ad."AdmissionUPITotal",ad."AdmissionOnlineTotal", ad."AdmissionChequeTotal",ad."AdmissionPaytmTotal", ad."AdmissionAmount",
						  lb."LabCash", lb."LabCard",lb."LabUPI", lb."LabOnline",lb."LabCheque", lb."LabPaytm",lb."LabAmount",
sum(pa."PharmaSaleCash") over(partition by a."ProviderId")  "PharmacySaleCash",
sum(pa."PharmaSaleCard") over(partition by a."ProviderId") 	"PharmacySaleCard",
sum(pa."PharmaSaleUPI") over(partition by a."ProviderId")  "PharmacySaleUPI",
sum(pa."PharmaSaleOnline") over(partition by a."ProviderId") 	"PharmacySaleOnline",
sum(pa."PharmaSaleCheque") over(partition by a."ProviderId")  "PharmacySaleCheque",
sum(pa."PharmaSalePaytm") over(partition by a."ProviderId") 	"PharmacySalePaytm",
sum(pa."PharmaAmount") over(partition by a."ProviderId") "PharmacyAmount",
coalesce(ap."AppointmentCashTotal",0)+	coalesce(ad."AdmissionCashTotal",0)+coalesce(lb."LabCash",0)+coalesce(sum(pa."PharmaSaleCash") over(partition by a."ProviderId") ,0) "TotalCash",
coalesce(ap."AppointmentCardTotal",0)+	coalesce(ad."AdmissionCardTotal",0)+coalesce(lb."LabCard",0)+coalesce(sum(pa."PharmaSaleCard") over(partition by a."ProviderId"),0)  "TotalCard",	
coalesce(ap."AppointmentUPITotal",0)+	coalesce(ad."AdmissionUPITotal",0)+coalesce(lb."LabUPI",0)+coalesce(sum(pa."PharmaSaleUPI") over(partition by a."ProviderId") ,0) "TotalUPI",
coalesce(ap."AppointmentOnlineTotal",0)+	coalesce(ad."AdmissionOnlineTotal",0)+coalesce(lb."LabOnline",0)+coalesce(sum(pa."PharmaSaleOnline") over(partition by a."ProviderId"),0)  "TotalOnline",	
coalesce(ap."AppointmentChequeTotal",0)+	coalesce(ad."AdmissionChequeTotal",0)+coalesce(lb."LabCheque",0)+coalesce(sum(pa."PharmaSaleCheque") over(partition by a."ProviderId") ,0) "TotalCheque",
coalesce(ap."AppointmentPaytmTotal",0)+	coalesce(ad."AdmissionPaytmTotal",0)+coalesce(lb."LabPaytm",0)+coalesce(sum(pa."PharmaSalePaytm") over(partition by a."ProviderId"),0)  "TotalPaytm",	

coalesce(ap."AppointmentAmount",0)+coalesce(ad."AdmissionAmount",0)+coalesce(lb."LabAmount",0)+coalesce(sum(pa."PharmaAmount") over(partition by a."ProviderId"),0) "Total"

from accountdata a

left join appointmentamount ap on ap."ProviderId"=a."ProviderId"
left join admissionamount ad on ad."ProviderId"=a."ProviderId"
left join LabAmount lb on lb."ProviderId"=a."ProviderId"
left join PharmaAmount pa on pa."ProviderId"=a."ProviderId"
;
	end
$BODY$;

ALTER FUNCTION public.udf_doctor_new_revenue(integer, integer, date, date)
    OWNER TO postgres;
